<?php

namespace app\api\logic;

use app\common\exception\BusinessException;
use app\common\model\AppConfig;
use app\common\model\User as UserModel;
use app\common\model\UserVip;
use app\common\model\UserVipCombo;
use app\common\model\UserVipTransfer;
use app\common\service\UserService;

class Vip
{

    /**
     * 转让说明
     */
    public const TRANSFER_NOTICE = [
        '1.本次操作手续费100积分',
        '2.请检查会员编号的正确性',
        '3.请检查您选择的会员正确性',
        '4.确认操作后不可撤回，请您谨慎操作',
    ];

    /**
     * 充值信息
     * @param $param
     * @return array
     * @throws
     */
    public function chargeInfo($param): array
    {
        // 提取参数
        $appId = getAppId();
        $userId = $param['userId'];

        // 检查是否虚拟支付
        if (isIosVirtualPay()) {
            throw new BusinessException(config('mcp.ios_not_support_notice'));
        }

        // 查询专业
        $professionInfo = app(UserModel::class)->getUserProfession($userId);
        if (!$professionInfo) {
            throw new BusinessException('未获取到你的专业信息,请重新选择下您的专业');
        }
        if (!$professionInfo['is_support_vip_combo']) {
            throw new BusinessException('您学习的专业仅支持通过单科购买');
        }

        // 查询套餐
        $vipComboList = app(UserVipCombo::class)->getListByAppId($appId);

        // 查询充值配置
        $appConfig = app(AppConfig::class);
        $config = $appConfig->getByIdAndType($appId, 'recharge_vip');

        // 组装专业信息
        $professionName = $professionInfo['name'];

        // 返回
        return [
            'professionName' => $professionName,
            'vipComboList' => $vipComboList,
            'vipChargeNoticeList' => $config['recharge_notice']
        ];
    }

    /**
     * 转让信息
     * @param $param
     * @return array
     * @throws
     */
    public function transferInfo($param): array
    {
        // 提取参数
        $userId = $param['userId'];

        // 获取有效会员
        $vip_list = app(UserVip::class)->getVipList($userId, true);
        foreach ($vip_list as $key => $val) {
            $vip_list[$key]['expire_name'] = date('Y-m-d', $val['expire']);
        }

        // 返回
        return [
            'vip_list' => $vip_list,
            'notice_list' => self::TRANSFER_NOTICE,
        ];
    }

    /**
     * 转让会员
     * @param $param
     * @throws
     */
    public function transfer($param)
    {
        // 提取参数
        $time = time();
        $sendUid = $param['userId'];
        $toUid = trim($param['uid']);
        $sendVid = trim($param['vid']);

        // 检查数据
        if ($sendUid == $toUid) {
            throw new BusinessException('无法转让会员给自己');
        }

        // 模型
        $vipModel = (new UserVip());
        $vipTransferModel = (new UserVipTransfer());
        $userService = new UserService();

        // 查询用户信息
        $where = ['id' => $toUid];
        $count = app(UserModel::class)->where($where)->field('id')->count();
        if (!$count) {
            throw new BusinessException('请检查会员编号是否正确');
        }

        // 查询VIP信息
        $where = [
            ['id', '=', $sendVid],
        ];
        $vidInfo = app(UserVip::class)->where($where)->find();
        $vidInfo = $vidInfo ? $vidInfo->toArray() : [];
        if (!$vidInfo) {
            throw new BusinessException('您选择的会员信息异常');
        }
        if ($vidInfo['expire'] <= $time) {
            throw new BusinessException('您选择的会员已经过期');
        }

        // 开始事务
        $vipModel->startTrans();

        // 计算自己的会员剩余有效期
        $sendExpire = ($vidInfo['expire'] - $time);

        // 扣除手续费
        $userService->deductScoreToUid($sendUid, 100, '会员过户');

        // 剩余时间充值到对方账户
        $vipModel->setExpire($toUid, $vidInfo['profession_id'], $sendExpire, 1);

        // 清空自己的会员
        $vipModel->deleteById($sendVid);

        // 记录日志
        $vipTransferModel->addLog($sendUid, $toUid, (array)$vidInfo);

        // 提交事务
        $vipModel->commit();
    }
}